package vn.co.thn.web.hotvideo.api;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
import com.google.gson.Gson;

import vn.co.thn.web.hotvideo.data.jpa.Video;
import vn.co.thn.web.hotvideo.json_object.Json_Video;
import vn.co.thn.web.hotvideo.model.VideoModel;

@Path("/video")
public class ApiVideo {
	private VideoModel videoModel = new VideoModel(20);

	@Path("/new")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getNew(@FormParam("page") int page) {
		videoModel.setPage(page);
		Json_Video json_Video = new Json_Video();
		json_Video.setVideo_list(videoModel.getNew());
		return new Gson().toJson(json_Video);
	}

	@Path("/item")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getItem(@FormParam("Key") String key) {
		return new Gson().toJson(videoModel.getObject(key));
	}

	@Path("/watched_most")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getWatchedMost(@FormParam("page") int page) {
		videoModel.setPage(page);
		Json_Video json_Video = new Json_Video();
		json_Video.setVideo_list(videoModel.getReview());
		return new Gson().toJson(json_Video);
	}

	@Path("/like_most")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getLikeMost(@FormParam("page") int page) {
		videoModel.setPage(page);
		Json_Video json_Video = new Json_Video();
		json_Video.setVideo_list(videoModel.getLike());
		return new Gson().toJson(json_Video);
	}

	@Path("/category")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getCategory(@FormParam("page") int page, @FormParam("category_key") String category_key) {
		videoModel.setPage(page);
		Json_Video json_Video = new Json_Video();
		json_Video.setVideo_list(videoModel.getCategory(category_key));
		return new Gson().toJson(json_Video);
	}

	@Path("/search")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getSearch() {
		return null;
	}

	@Path("/my_list")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getMyList(@FormParam("page") int page, @FormParam("deviceid") String DeviceId) {
		videoModel.setPage(page);
		Json_Video json_Video = new Json_Video();
		json_Video.setVideo_list(videoModel.getDevice(DeviceId));
		return new Gson().toJson(json_Video);
	}

	@Path("/set_view")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String SetView(@FormParam("Key") String key) {
		Video video = videoModel.getObject(key);
		video.setViewcount(video.getViewcount() + 1);
		videoModel.Update(video);
		return new Gson().toJson(video);
	}

	@Path("/set_like")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String setLike(@FormParam("Key") String key) {
		Video video = videoModel.getObject(key);
		video.setLikecount(video.getLikecount() + 1);
		videoModel.Update(video);
		return new Gson().toJson(video);
	}

	@Path("/list_search")
	@POST
	@Produces("application/json")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String list_search(@FormParam("page") int page, @FormParam("keywork") String key_word) {
		videoModel.setPage(page);
		videoModel.setFilter("Content_Search >= :1 && Content_Search < :2 ");
		Json_Video json_Video = new Json_Video();
		json_Video.setVideo_list(videoModel.SearchFiled(key_word.toUpperCase()));
		return new Gson().toJson(json_Video);
	}

	public String VideoError(@FormParam("Key_list") String key_list) throws JSONException {
		JSONObject jsonObject = new JSONObject(key_list);
		JSONArray jsonArray = jsonObject.getJSONArray("list");
		for(int i=0;i<jsonArray.length();i++){
			JSONObject obj=jsonArray.getJSONObject(i);
			
			Video video = videoModel.getObject(obj.get("key"));
			video.setError(1);
			videoModel.Update(video);
		}
		return "";
	}
}
